<?php

global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $fields='';
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishopzufang_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$good=pdo_get('jy_weishop_goods',array('id'=>$v['goodsid']),array('title','image','price'));
		$v['image']=$good['image'];
		$v['timeid']=$v['timeid'].'点';
		$v['title']=$good['title'];
		$v['price']=$good['price'];
		$v['grouptitle']=pdo_getcolumn('jy_weishopzufang_group',array('id'=>$v['groupid']),'title');
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'房屋管理'),
			array('op'=>'edit','title'=>'房屋添加'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'房屋名称','type'=>'string'),
			array('name'=>'image','title'=>'房屋图片','type'=>'image'),
			array('name'=>'price','title'=>'房屋价格','type'=>'string'),
			array('name'=>'grouptitle','title'=>'分类','type'=>'string'),	
			array('name'=>'huxing','title'=>'户型','type'=>'string'),
			array('name'=>'area','title'=>'面积','type'=>'string'),
			
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishopzufang_goods',array('id'=>$id));
		$item['pcd']=json_decode($item['pcd'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'会场设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'房屋管理'),
			array('op'=>'edit','title'=>'房屋添加'),
		),
		'edit'=>array(
			array('name'=>'groupid','title'=>'选择分类','type'=>'select','options'=>'groups'),
			array('type'=>'choose','title'=>'选择房屋','name'=>'goodsid','action'=>'jy_weishop/goods.get_goods/','choose_title'=>'选择房屋','help'=>'为了同时支持商城的各种活动，采用选择绑定商品的方式！'),
			array('name'=>'area','title'=>'房屋面积','type'=>'unitstring','unit'=>'平'),
			array('name'=>'huxing','title'=>'户型','type'=>'string'),
			array('name'=>'pcd','title'=>'区域','type'=>'pcd'),
			array('name'=>'address','title'=>'地址','type'=>'string'),			
			array('name'=>'outnum','title'=>'押几个月','type'=>'unitstring','unit'=>'月'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['groups']=pdo_getall('jy_weishopzufang_group',array('uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$id=intval($postdata['id']);
	if(!$postdata['goodsid']){
		exi('请关联商品','error');
	}
	if(!$postdata['groupid']){
		exi('请选择分类','error');
	}
	$upgoods=array();
	$upgoods['goodsid']=$postdata['goodsid'];
	$upgoods['area']=$postdata['area'];
	$upgoods['huxing']=$postdata['huxing'];
	$upgoods['pcd']=ijson_encode($postdata['pcd']);
	$upgoods['address']=$postdata['address'];
	$upgoods['groupid']=intval($postdata['groupid']);
	$upgoods['outnum']=intval($postdata['outnum']);
	$upgoods['status']=intval($postdata['status']);
	$taskid=pdo_getcolumn('jy_seckill_task_room',array('id'=>$_SESSION['roomid']),'taskid');
	model('jy_weishop/goods')->up_pluginname($upgoods['goodsid'],'jy_weishopzufang');
	if($id){
		pdo_update('jy_weishopzufang_goods',$upgoods,array('id'=>$id));
	}else{
		$upgoods['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishopzufang_goods',$upgoods);
	}
	exi('设置成功！','','/goods/list/');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishopzufang_goods',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishopzufang_goods',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>